var nowPoint = null
var map = null
var marker1 = null

setInterval(function () {
    var date = new Date();
    var year = date.getFullYear(); //获取当前月份  
    var mon = date.getMonth() + 1; //获取当前月份  
    var day = date.getDate(); //获取当前日  
    var h = date.getHours(); //获取小时  
    var m = date.getMinutes(); //获取分钟  
    var s = date.getSeconds(); //获取秒  
    if (mon < 10) mon = "0" + mon;
    if (h < 10) h = "0" + h;
    if (m < 10) m = "0" + m;
    if (s < 10) s = "0" + s;


    var current_time = '#' +
        year + '-' + mon + '-' + day + ' ' + h + ':' + m

    $(".ub-time").html(current_time)
}, 1000)

var vue = new Vue({
    el: "#main",
    data() {
        return {
            userInfo: {},
            id: 1, //网格的id
            type: '',
            isManual: false, //获取场所地址的dia
            isUploadLoading: false, //上传图片的loading
            isUpload_Loading: false, //是否正在上传照片
            isUploadNoteImg: false, //上传图片的loading
            isUpload_NoteImg: false, //是否正在上传照片
            loading: false,
            prompt: false, //提示弹窗
            msg: '', //提示弹窗的内容
            imgNumber: 0, //图片上传的个数
            noteImgNumber: 0, //通话图片上传的个数
            time: null,
            dateShow: false,
            last_care_id: '', //暂存的id
            statused: '', //是否有暂存
            ruleForm: {
                address: '', //地址
                content: '', //走访情况记录
                images: [], //上传照片
                imagesPath: [], //上传照片path
                phoneImages: [], //上传电话探访照片
                phoneImagesPath: [], //上传电话探访照片path
                longitude: '', //经度
                latitude: '', //维度
                birthplaceQ: '', //电话上方时间戳
                _birthplaceQ: '', //电话上访时间
            },
            location: '',
            rules: {
                address: [{
                    required: true,
                    message: ' '
                }],
                content: [{
                    required: true,
                    message: ' '
                }],
            },
            isShow: false,
            isShowAll: false,
        }
    },
    created() {
        this.id = getRequest().id
        this.type = getRequest().type
        this.last_care_id = getRequest().last_care_id
        this.statused = getRequest().status

        if (this.type == 3) {
            this.rules.address[0].required = false
            if (this.statused == 0) {
                this.getTemporary()
            }

        }

        this.getSeekManInfo()
    },
    mounted() {

    },
    methods: {
        // 获取老人详情
        getSeekManInfo() {
            ajax({
                url: formal + '/sick-man/get-sick-man/' + this.id,
                type: 'GET',
                success: (res) => { //请求成功回调
                    this.userInfo = res
                },
            })
        },
        // 获取暂存数据
        getTemporary() {
            ajax({
                url: formal + '/sick-man/get-sick-care/' + this.last_care_id,
                type: 'GET',
                success: (res) => { //请求成功回调
                    console.log(res);
                    this.ruleForm.address = res.address
                    this.ruleForm.content = res.content
                    this.ruleForm.longitude = res.map_y
                    this.ruleForm.latitude = res.map_x
                    this.ruleForm._birthplaceQ = res._phone_time_datetime
                    res.image_list.map(item => {
                        this.ruleForm.images.push(item.url)
                        this.ruleForm.imagesPath.push(item.path)
                    })
                    res.phone_image_list.map(item => {
                        this.ruleForm.phoneImages.push(item.url)
                        this.ruleForm.phoneImagesPath.push(item.path)
                    })
                    this.ruleForm.address = res.address
                },
            })
        },
        // 上传图片判断上传长度，判断图片上传1or备注图片上传2
        uploadImageBtn(type) {
            let imgNum = 0
            if (type == 1) {
                this.imgNumber = 9 - this.ruleForm.images.length
                if (this.imgNumber == 0) {
                    this.isUploadLoading = false
                }
                imgNum = this.imgNumber
            } else if (type == 2) {
                this.noteImgNumber = 9 - this.ruleForm.phoneImages.length
                if (this.noteImgNumber == 0) {
                    this.isUploadNoteImg = false
                }
                imgNum = this.noteImgNumber
            }
            wx.ready(() => {
                wx.chooseImage({
                    count: imgNum, // 默认9
                    sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有
                    sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有
                    success: (res) => {
                        var localIds = res
                            .localIds; // 返回选定照片的本地 ID 列表，localId可以作为 img 标签的 src 属性显示图片
                        if (type == 1) {
                            this.isUpload_Loading = true
                        } else {
                            this.isUpload_NoteImg = true
                        }
                        this.uploadImg(localIds, type);
                    }
                })
            })
        },
        // 多图上传，判断上传长度执行回调
        uploadImg(localIds, type) {
            if (type == 1) {
                this.isUploadLoading = true
            } else {
                this.isUploadNoteImg = true
            }
            var localId = localIds.shift();
            wx.uploadImage({
                localId: localId, // 需要上传的图片的本地ID，由 chooseImage 接口获得
                isShowProgressTips: 0, // 默认为1，显示进度提示
                success: (res) => {
                    var serverId = res.serverId; // 返回图片的服务器端ID
                    this.downloadImg(serverId, type)
                    if (localIds.length > 0) {
                        this.uploadImg(localIds, type)
                    } else {
                        if (type == 1) {
                            this.isUploadLoading = false
                            this.isUpload_Loading = false
                        } else {
                            this.isUploadNoteImg = false
                            this.isUpload_NoteImg = false
                        }
                    }
                }
            })
        },
        // 上传成功后调取接口，获取图片地址做展示
        downloadImg(serverId, type) {
            ajax({
                url: formal + '/files/get-wx-media',
                type: 'POST',
                async: false,
                data: {
                    mediaId: serverId
                },
                success: (res) => {
                    console.log(res);
                    if (res.status == 1) {
                        if (type == 1 && this.ruleForm.images.length <= 8) {
                            this.ruleForm.images.push(res.data.url)
                            this.ruleForm.imagesPath.push(res.data.local)
                            this.imgNumber = 9 - this.ruleForm.images.length
                            this.isUploadLoading = false
                        } else if (type == 2 && this.ruleForm.phoneImages.length <= 8) {
                            this.ruleForm.phoneImages.push(res.data.url)
                            this.ruleForm.phoneImagesPath.push(res.data.local)
                            this.noteImgNumber = 9 - this.ruleForm.phoneImages.length
                            this.isUploadNoteImg = false
                        }
                    }
                }
            })
        },
        // 查看图片
        lookImg(list) {
            wx.previewImage({
                // current: 'http://admin.tangshan.youbo.me/upload/place/202211/7WBay02UiZx9mszwLJcjoEgrX5GYhPTV.jpg', // 当前显示图片的 http 链接
                urls: list // 需要预览的图片 http 链接列表
            });
        },
        // 删除新增图片
        deleteImage(index, type) {
            if (type == 1) {
                this.ruleForm.images.splice(index, 1)
                this.ruleForm.imagesPath(index, 1)
                this.imgNumber = 9 - this.ruleForm.images.length
            } else if (type == 2) {
                this.ruleForm.phoneImages.splice(index, 1)
                this.ruleForm.phoneImagesPath(index, 1)
                this.noteImgNumber = 9 - this.ruleForm.phoneImages.length
            }
        },
        // 调用微信sdk-wx.getLocation获取当前位置
        getLocation() {
            // this.isManual = true
            // 获取当前位置
            wx.getLocation({
                isHighAccuracy: true,
                type: 'gcj02', // 默认为wgs84的 gps 坐标，如果要返回直接给 openLocation 用的火星坐标，可传入'gcj02'
                complete: (res) => {
                    this.ruleForm.latitude = res.latitude; // 纬度，浮点数，范围为90 ~ -90
                    this.ruleForm.longitude = res.longitude; // 经度，浮点数，范围为180 ~ -180。
                    ajax({
                        url: formal + `/sick-man/get-address?location=${res.latitude},${res.longitude}`,
                        type: 'GET',
                        success: (res) => {
                            this.ruleForm.address = res
                        }
                    })

                    // this.getMap(currentData)
                },
            });
        },
        // 获取当前中心点位置+地图移动事件
        getMap(res) {
            nowPoint = res
            //初始化地图对象
            if (map == null) {
                map = new T.Map("container");
            }
            //设置显示地图的中心点和级别
            map.centerAndZoom(new T.LngLat(res[0], res[1]), 18);
            map.setMinZoom(11);
            map.setMaxZoom(18);
            map.addEventListener("moveend", function (e) {
                nowPoint = [map.getCenter().getLng(), map.getCenter().getLat()]
            })
        },
        // 跳转到当前位置，给当前位置添加点标注
        goCurrent() {
            if (this.time) {
                clearTimeout(this.time)
            }
            this.time = setTimeout(() => {
                this.removeMarker()
                wx.getLocation({
                    isHighAccuracy: true,
                    type: 'gcj02', // 默认为wgs84的 gps 坐标，如果要返回直接给 openLocation 用的火星坐标，可传入'gcj02'
                    complete: (res) => {
                        let currentData = gcj02ToWgs(res.longitude, res.latitude)
                        //创建图片对象
                        var icon = new T.Icon({
                            iconUrl: "../images/coordinates.png",
                            iconSize: new T.Point(25, 25),
                            // iconAnchor: new T.Point(10, 25)
                        });
                        marker1 = null
                        marker1 = new T.Marker(new T.LngLat(currentData[0], currentData[
                            1]), {
                            icon: icon
                        });
                        map.addOverLay(marker1);
                        map.centerAndZoom(new T.LngLat(currentData[0], currentData[1]),
                            18);
                    },
                });
            }, 500)
        },
        // 确定中心点经纬度按钮
        confirmLocation() {
            this.isManual = false
            this.ruleForm.latitude = nowPoint[1]; // 纬度，浮点数，范围为90 ~ -90
            this.ruleForm.longitude = nowPoint[0]; // 经度，浮点数，范围为180 ~ -180。
            this.location = "经度:" + this.ruleForm.longitude + "   纬度:" + this.ruleForm.latitude
        },
        // 新增场所方法
        submit() {
            this.loading = true
            if (this.ruleForm.content == "") {
                this.prompt = true
                this.loading = false
                this.msg = '请输入走访情况记录'
                return
            }

            let obj = {}
            if (this.type == 1) {
                if (this.ruleForm.longitude == "") {
                    this.prompt = true
                    this.loading = false
                    this.msg = '请获取当前位置'
                    return
                } else if (this.ruleForm.images.length == 0) {
                    this.prompt = true
                    this.loading = false
                    this.msg = '关爱照片不能为空'
                    return
                }
                obj = {
                    status: 1,
                    man_id: this.id,
                    content: this.ruleForm.content,
                    address: this.ruleForm.address,
                    imageList: this.ruleForm.imagesPath,
                    map_x: this.ruleForm.latitude,
                    map_y: this.ruleForm.longitude,
                }
            } else if (this.type == 2) {
                if (this.ruleForm.birthplaceQ == "") {
                    this.prompt = true
                    this.loading = false
                    this.msg = '请选电话择探访时间'
                    return
                } else if (this.ruleForm.phoneImages.length == 0) {
                    this.prompt = true
                    this.loading = false
                    this.msg = '关爱通话照片不能为空'
                    return
                }
                obj = {
                    status: 1,
                    man_id: this.id,
                    content: this.ruleForm.content,
                    phone_time: this.ruleForm.birthplaceQ,
                    phoneImageList: this.ruleForm.phoneImagesPath,
                }
            } else {
                // if (this.ruleForm.longitude == "") {
                //     this.prompt = true
                //     this.loading = false
                //     this.msg = '请获取当前位置'
                //     return
                // } else if (this.ruleForm.images.length == 0) {
                //     this.prompt = true
                //     this.loading = false
                //     this.msg = '关爱照片不能为空'
                //     return
                // } else if (this.ruleForm.birthplaceQ == "") {
                //     this.prompt = true
                //     this.loading = false
                //     this.msg = '请选择电话探访时间'
                //     return
                // } else if (this.ruleForm.phoneImages.length == 0) {
                //     this.prompt = true
                //     this.loading = false
                //     this.msg = '关爱通话照片不能为空'
                //     return
                // }
                obj = {
                    status: 1,
                    man_id: this.id,
                    content: this.ruleForm.content,
                    address: this.ruleForm.address,
                    imageList: this.ruleForm.imagesPath,
                    map_x: this.ruleForm.latitude,
                    map_y: this.ruleForm.longitude,
                    phone_time: this.ruleForm.birthplaceQ,
                    phoneImageList: this.ruleForm.phoneImagesPath,
                }
                if (this.statused == 0) {
                    obj.id = this.last_care_id
                }
            }

            ajax({
                url: formal + '/sick-man/create-sick-man-care',
                type: "POST",
                data: obj,
                success: (res) => {
                    this.msg = '关爱成功'
                    this.prompt = true
                }
            })
        },
        // 暂存
        temporary() {
            this.loading = true
            if (this.ruleForm.content == "") {
                this.prompt = true
                this.loading = false
                this.msg = '请输入走访情况记录'
                return
            }

            let obj = {
                status: 0,
                man_id: this.id,
                content: this.ruleForm.content,
                address: this.ruleForm.address,
                imageList: this.ruleForm.imagesPath,
                map_x: this.ruleForm.latitude,
                map_y: this.ruleForm.longitude,
                phone_time: this.ruleForm.birthplaceQ,
                phoneImageList: this.ruleForm.phoneImagesPath,
            }

            ajax({
                url: formal + '/sick-man/create-sick-man-care',
                type: "POST",
                data: obj,
                success: (res) => {
                    this.msg = '暂存成功'
                    this.prompt = true
                    this.loading = false
                }
            })
        },
        // 判断当前位置是否有图标，有就清除
        removeMarker() {
            if (marker1 != null) {
                map.removeOverLay(marker1);
                marker1 = null
            }
        },
        // 提示弹窗
        goPlaceMan() {
            if (this.msg == "关爱成功" || this.msg == "暂存成功") {
                this.prompt = false
                history.back()
            } else {
                this.prompt = false
            }
            this.loading = false
        },
        // 时间选择器确定事件
        dateTimeConfirm(e) {
            let time = new Date(e)
            let t = time.getTime()
            let year = time.getFullYear()
            let mon = time.getMonth() + 1
            mon = mon > 9 ? mon : '0' + mon
            let day = time.getDate()
            day = day > 9 ? day : '0' + day
            let h = time.getHours()
            h = h > 9 ? h : '0' + h
            let min = time.getMinutes()
            min = min > 9 ? min : '0' + min
            let s = time.getSeconds()
            s = s > 9 ? s : '0' + s
            let str = year + '-' + mon + '-' + day + ' ' + h + ':' + min + ':' + s
            this.ruleForm._birthplaceQ = str
            this.ruleForm.birthplaceQ = t / 1000
            this.dateShow = false
        },
    }
})